<?php
class EPMenuListRenderer extends EPMenuRenderer{
	function render($root){
		return $this->renderListRecursive($root);
	}
	
	private function renderItemRecursive($menuItem){
		if(!$menuItem->visible){
			return '';
		}
		$linkHtmlOptions=$menuItem->linkHtmlOptions;
		$linkHtmlOptions['class']=(isset($linkHtmlOptions['class'])?$linkHtmlOptions['class'].' ':'').implode(' ',$menuItem->getLinkCss());
		if(!empty($menuItem->url)){
		$menuHtml=CHtml::link($menuItem->label,$menuItem->url,$linkHtmlOptions);
		}else{
			$menuHtml=CHtml::tag('span',$linkHtmlOptions,$menuItem->label);
		}
		if($menuItem->hasSubMenu()){
			$submenu=$menuItem->getSubMenu();
			$menuHtml.=$this->renderListRecursive($submenu);
			
		}
		$htmlOptions=$menuItem->listItemHtmlOptions;
		if(!empty($menuItem->itemId)){
			$htmlOptions['id']=$menuItem->itemId;
		}
		$htmlOptions['class']=(isset($htmlOptions['class'])?$htmlOptions['class'].' ':'').implode(' ',$menuItem->getItemCss());
		return CHtml::tag('li',$htmlOptions,$menuHtml);
	}
	
	private function renderListRecursive($itemList){
		$listHtmlOptions=$itemList->menuHtmlOptions;
		if(!empty($itemList->menuId)){
			$listHtmlOptions['id']=$itemList->menuId;
		}
		$listHtmlOptions['class']=(isset($listHtmlOptions['class'])?$listHtmlOptions['class'].' ':"").implode(' ',$itemList->getMenuCss());
		$listHtml='';
		foreach($itemList->getMenuItems() as $item){
			$listHtml.=$this->renderItemRecursive($item);
		}
		return CHtml::tag('ul',$listHtmlOptions,$listHtml);
	}
}